package demo.pattern.singleton;

/**
 * @Author: yu.zhou
 * @DateTime: 2023/01/04 13:44
 */
public class LazyDoubleCheckSingleton {

    private volatile static LazyDoubleCheckSingleton instance;

    private LazyDoubleCheckSingleton() {
    }

    public static LazyDoubleCheckSingleton getInstance() {
        // 第一次检测
        if (instance == null) {
            synchronized (LazyDoubleCheckSingleton.class) {
                if (instance == null) {
                    instance = new LazyDoubleCheckSingleton();
                    // 1.分配空间
                    // 2.实例化
                    // 3.初始化
                    // 以上三步在不使用volatile的情况下,cpu会对其进行指令重排
                }
            }
        }
        return instance;
    }

}
